#include "cfg.h"

#include <QApplication>
#include <QMutexLocker>
#include <QSettings>
#include <QDebug>

QScopedPointer<Cfg> Cfg::m_self;
QMutex Cfg::m_ptrMutex;

Cfg::Cfg()
    : m_cfgFile()
{
    QString qsCfgPath = QCoreApplication::applicationDirPath();
    qsCfgPath += "/config/cfg.ini";
    m_cfgFile = qsCfgPath;
}

QScopedPointer<Cfg> & Cfg::GetInstance()
{
    if (m_self.isNull())
    {
        QMutexLocker lock(&m_ptrMutex);
        if (m_self.isNull())
        {
            m_self.reset(new Cfg());
        }
    }

    return m_self;
}

Cfg::~Cfg()
{
    qDebug() << "delete Cfg";
}

QString Cfg::ReadScreenshotShortcut()
{
    QScopedPointer<Cfg>& ptr = Cfg::GetInstance();

    QString qsKeySeq;
    if (!ptr.isNull())
    {
        QSettings settings(ptr->GetFilePath(), QSettings::IniFormat);
        qsKeySeq = settings.value("shortcut/screenshot", "").toString();
    }

    return qsKeySeq;
}

bool Cfg::SaveScreenshotShortcut(const QString &qsKeySeq)
{
    QScopedPointer<Cfg>& ptr = Cfg::GetInstance();

    if (!ptr.isNull())
    {
        QSettings settings(ptr->GetFilePath(), QSettings::IniFormat);
        settings.setValue("shortcut/screenshot", qsKeySeq);
    }

    return true;
}
